package com.chrray.position.mygenerator.htmlutil;
import com.chrray.position.mygenerator.entity.Table;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HtmlUtil {


	/**
	 * 复制模板文件（共用zcurd的模板）
	 */
	public static void copyTemp(Map<String, String> configMap, List<Table> tableList, String fileName, String newFileName) {
		for (Table table : tableList) {
		//获取service的路径
		String baseUrlPackageName = configMap.get("baesHtmlPackage");
		//生成实体的包路径（去掉磁盘的盘符路径，获取相对路径）
		String baseHtmlPackagePath =baseUrlPackageName.replaceAll("\\.", "/");
		String content = StringUtils.readTxt2String(new File(baseHtmlPackagePath + fileName));
		//对特定标签进行替换，使freemark不解析
		//替换include标签
		content = content.replaceAll("<#include", "\\${\"<\"}#include");
		//替换字典数据输出
		content = content.replace("<#list item.dict.keySet() as key>,{id:'${key}', text:'${item.dict.get(key)}'}</#list>",
				"<${'#'}list dictData${item.field_name}.keySet() as key>,{id:'${'$'}{key}', text:'${'$'}{dictData${item.field_name}.get(key)}'}</${'#'}list>");

		if("addPage.html".equals(fileName) || "updatePage.html".equals(fileName) || "detailPage.html".equals(fileName)) {
			//删除块
			content = content.replace("\"headId\": ${headId},", "");
			//替换主键、model输出
			content = content.replace("model[item.field_name]??", "1==1");	//临时处理，默认都显示图片
			content = content.replace("${model[head.id_field]}", "${'$'}{model.${head.id_field}}");
			content = content.replaceAll("\\$\\{model\\[item\\.field_name\\]", "\\${'\\$'}{model.\\${item.field_name}");
			content = content.replaceAll("\\$\\{modelDetail\\[item\\.field_name\\]", "\\${'\\$'}{modelDetail.\\${item.field_name}");
		}
		//StringUtils.saveToFile(baseHtmlPackagePath + "getCode/" + fileName, content);
		//获取service的路径
		String saveHtmlPackageName = configMap.get("saveHtmlPackage");
		String saveHtmlPackagePath =saveHtmlPackageName.replaceAll("\\.", "/");
		String genCodePagePath = saveHtmlPackagePath;
        /*new File(genCodePagePath).mkdirs();
        new File(genCodePagePath).mkdirs();*/
		if(!new File(genCodePagePath +  newFileName).exists()){
			String[] classNameStr = table.getName().split("_");
			String className="";
			className+=classNameStr[0].substring(0, 1).toLowerCase()+classNameStr[0].substring(1);
			for(int i=1;i<classNameStr.length;i++){
				className+=classNameStr[i].substring(0, 1).toUpperCase()+classNameStr[i].substring(1);
			}

			Map<String, Object> mateDate =new HashMap<String, Object>();
			mateDate.put("className", className);
			mateDate.put("queryPara", new HashMap<>());
			try {
				File packageFile = new File(saveHtmlPackagePath +className);
				if (!packageFile.exists()) {
					packageFile.mkdirs();
				}
				StringUtils.saveToFile(saveHtmlPackagePath +className+"/"+newFileName, content);
				StringUtils.gen(mateDate, baseHtmlPackagePath + "getCode/"+fileName, saveHtmlPackagePath +className+"/"+newFileName);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	       	 System.out.println("文件["+genCodePagePath +  fileName+"]生成成功!");
	       }else{
	    	   System.out.println("文件["+genCodePagePath +  fileName+"]已经存在!");
	       }
		}
	}
}
